# ----------------------------- #
#    Master replication file    #
# ----------------------------- #
# Edgerton (2023) JOP
# R version 4.2.2 (2022-10-31 ucrt)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 22621)
# 
# Matrix products: default
# 
# locale:
#   [1] LC_COLLATE=English_United States.utf8 
# [2] LC_CTYPE=English_United States.utf8   
# [3] LC_MONETARY=English_United States.utf8
# [4] LC_NUMERIC=C                          
# [5] LC_TIME=English_United States.utf8    
# 
# attached base packages:
#   [1] parallel  stats     graphics  grDevices utils    
# [6] datasets  methods   base     
# 
# other attached packages:
#   [1] glmnet_4.1-8        missForest_1.5     
# [3] Metrics_0.1.4       precrec_0.14.2     
# [5] speedglm_0.3-5      biglm_0.9-2.1      
# [7] DBI_1.1.3           caret_6.0-94       
# [9] lattice_0.21-8      tseries_0.10-53    
# [11] forecast_8.21       vars_1.5-9         
# [13] lmtest_0.9-40       urca_1.3-3         
# [15] strucchange_1.5-3   sandwich_3.0-2     
# [17] zoo_1.8-10          MASS_7.3-58.3      
# [19] doFuture_1.0.0      future_1.32.0      
# [21] foreach_1.5.2       cowplot_1.1.1      
# [23] assortnet_0.20      plotROC_2.3.0      
# [25] ggplot2_3.4.2       tidyr_1.2.0        
# [27] pROC_1.18.0         optimx_2022-4.30   
# [29] fixest_0.10.4       ClusterR_1.3.0     
# [31] cluster_2.1.4       tibble_3.2.1       
# [33] reshape2_1.4.4      intergraph_2.0-2   
# [35] purrr_1.0.2         tidygraph_1.2.3    
# [37] alpaca_0.3.4        network_1.18.1     
# [39] readxl_1.4.1        haven_2.5.0        
# [41] data.table_1.14.2   btergm_1.10.6      
# [43] brms_2.19.0         Rcpp_1.0.9         
# [45] rstudioapi_0.15.0   texreg_1.38.6      
# [47] countrycode_1.4.0   igoR_0.1.4         
# [49] lme4_1.1-32         Matrix_1.5-4       
# [51] peacesciencer_1.1.0 igraph_1.3.4       
# [53] dplyr_1.1.1         plyr_1.8.7         
# 
# loaded via a namespace (and not attached):
#   [1] utf8_1.2.2              tidyselect_1.2.0       
# [3] htmlwidgets_1.6.2       grid_4.2.2             
# [5] gmp_0.7-1               munsell_0.5.0          
# [7] codetools_0.2-19        DT_0.28                
# [9] miniUI_0.1.1.1          withr_2.5.0            
# [11] Brobdingnag_1.2-9       colorspace_2.1-0       
# [13] lpSolveAPI_5.5.2.0-17.9 knitr_1.43             
# [15] stats4_4.2.2            ROCR_1.0-11            
# [17] robustbase_0.95-1       stevemisc_1.6.0        
# [19] TTR_0.24.3              bayesplot_1.10.0       
# [21] listenv_0.9.0           Rdpack_2.5             
# [23] rstan_2.26.22           farver_2.1.1           
# [25] bridgesampling_1.1-2    coda_0.19-4            
# [27] parallelly_1.36.0       vctrs_0.6.3            
# [29] generics_0.1.3          ipred_0.9-14           
# [31] xfun_0.40               itertools_0.1-3        
# [33] randomForest_4.7-1.1    R6_2.5.1               
# [35] markdown_1.8            arm_1.13-1             
# [37] cachem_1.0.7            promises_1.2.0.1       
# [39] scales_1.2.1            nnet_7.3-19            
# [41] gtable_0.3.4            globals_0.16.2         
# [43] processx_3.8.0          timeDate_4022.108      
# [45] rlang_1.1.1             splines_4.2.2          
# [47] ModelMetrics_1.2.2.2    trust_0.1-8            
# [49] checkmate_2.1.0         inline_0.3.19          
# [51] abind_1.4-5             threejs_0.3.3          
# [53] crosstalk_1.2.0         backports_1.4.1        
# [55] quantmod_0.4.25         httpuv_1.6.9           
# [57] lava_1.7.2.1            tensorA_0.36.2         
# [59] tools_4.2.2             statnet.common_4.8.0   
# [61] ellipsis_0.3.2          posterior_1.4.1        
# [63] base64enc_0.1-3         ps_1.7.4               
# [65] prettyunits_1.1.1       rpart_4.1.19           
# [67] dreamerr_1.3.0          fracdiff_1.5-2         
# [69] magrittr_2.0.3          ergm_4.4.0             
# [71] sna_2.7-1               colourpicker_1.3.0     
# [73] mvtnorm_1.1-3           matrixStats_0.63.0     
# [75] hms_1.1.3               shinyjs_2.1.0          
# [77] mime_0.12               evaluate_0.21          
# [79] xtable_1.8-4            shinystan_2.6.0        
# [81] shape_1.4.6             gridExtra_2.3          
# [83] rstantools_2.3.1.1      compiler_4.2.2         
# [85] V8_4.3.3                crayon_1.5.2           
# [87] minqa_1.2.5             StanHeaders_2.26.27    
# [89] htmltools_0.5.5         later_1.3.0            
# [91] Formula_1.2-5           RcppParallel_5.1.7     
# [93] lubridate_1.8.0         boot_1.3-28.1          
# [95] cli_3.6.1               quadprog_1.5-8         
# [97] rbibutils_2.2.13        gower_1.0.1            
# [99] forcats_1.0.0           pkgconfig_2.0.3        
# [101] geosphere_1.5-18        numDeriv_2016.8-1.1    
# [103] sp_1.6-0                recipes_1.0.7          
# [105] dygraphs_1.1.1.6        hardhat_1.3.0          
# [107] rngtools_1.5.2          prodlim_2023.03.31     
# [109] doRNG_1.8.6             stringr_1.5.0          
# [111] distributional_0.3.2    callr_3.7.3            
# [113] digest_0.6.29           rle_0.9.2              
# [115] rmarkdown_2.24          cellranger_1.1.0       
# [117] curl_5.0.0              shiny_1.7.5            
# [119] gtools_3.9.4            nloptr_2.0.3           
# [121] lifecycle_1.0.3         nlme_3.1-162           
# [123] jsonlite_1.8.7          fansi_1.0.3            
# [125] labelled_2.12.0         pillar_1.9.0           
# [127] loo_2.6.0               survival_3.5-5         
# [129] fastmap_1.1.1           httr_1.4.7             
# [131] DEoptimR_1.1-1          pkgbuild_1.4.2         
# [133] glue_1.6.2              xts_0.13.0             
# [135] shinythemes_1.2.0       iterators_1.0.14       
# [137] class_7.3-21            stringi_1.7.8          
# [139] memoise_2.0.1           future.apply_1.11.0    

## Clear working space
rm(list = ls())

## Check packages
packages <- c('plyr', 'dplyr', 'igraph', "peacesciencer",
              'lme4', 'igoR', "countrycode", 'texreg', 'rstudioapi',
              'brms', "btergm", "stats", "data.table", 
              'haven', 'readxl', 'Matrix', 'network', 'alpaca',
              'tidygraph', 'purrr', 'intergraph', 'reshape2',
              'network', 'tibble', 'cluster', 'ClusterR',
              'fixest', 'optimx', 'pROC', 'tidyr', 'parallel',
              'plotROC', 'ggplot2', 'texreg', 'plotROC', 'assortnet',
              'cowplot', 'doFuture', 'parallel', 'fixest',
              'vars', 'forecast', 'tseries', 'caret', 'speedglm',
              'precrec', 'Metrics', 'missForest', 'tidyr', 'glmnet')


# renv::restore()


## Load missing packages
if (length(setdiff(packages, rownames(installed.packages()))) != 0)
{
  install.packages(setdiff(packages, rownames(installed.packages())))    
}
lapply(packages, library, character.only = T)



## Set your personal working directory
working_directory <- "cooperative_regimes/data_processing"
if (!grepl(working_directory, getwd()))
{
  setwd(working_directory)  
}


## Create the network layer data
suppressMessages({
  suppressWarnings({
    source("1_create_network_data_replication.r")
  })
})
gc()  

## 2. Weight the network edges
suppressMessages({
  suppressWarnings({
    source("2_weight_data_replication.r")
  })
})
gc()  

## 3. Impute missing values
suppressMessages({
  suppressWarnings({
    source("3_impute_democracy_data_replication.r")
  })
})
gc()  

## 4. Create diffusion data
suppressMessages({
  suppressWarnings({
    source("4_create_diffusion_data_replication.r")
  })
})
gc()  

## 5. Analyze institutional diffusion
suppressMessages({
  suppressWarnings({
    source("5_analyze_institutional_diffusion_data_replication.r")
  })
})
gc()  

## 6. Create conflict data
suppressMessages({
  suppressWarnings({
    source("6_create_conflict_data_replication.r")
  })
})
gc()  

## 7. Analyze Conflict data
suppressMessages({
  suppressWarnings({
    source("7_analyze_conflict_data_replication.r")
  })
})
gc()  

## 8. Create assortativity date
suppressMessages({
  suppressWarnings({
    source("8_create_assortativity_measures_replication.r")
  })
})
gc()  

## 9. Analyze time series data
suppressMessages({
  suppressWarnings({
    source("9_analyze_systemic_levels_replication.r")
  })
})
gc()  

## 10. Alterantive community detection
suppressMessages({
  suppressWarnings({
    source("10_alt_comm_detection_replication.r")
  })
})
gc()  

## 11. Leiden replication
suppressMessages({
  suppressWarnings({
    source("11_leiden_replication.r")
  })
})
gc()  

## 12. Alternative downweight schemes and layers
suppressMessages({
  suppressWarnings({
    source("12_testing_formula_downweight_replication.r")
  })
})
gc()  

## 13. Alteranative downweight schemes and layers as an average of tie
suppressMessages({
  suppressWarnings({
    source("13_testing_formula_downweight_average_replication.r")
  })
})
gc() 

## 14. Data viz and tables
suppressMessages({
  suppressWarnings({
    source("14_data_visualization_tables_replication.r")
  })
})
gc()  

## 15. Model fits TERGM and Bayes
suppressMessages({
  suppressWarnings({
    source("15_model_fit_bayes_replication.r")
  })
})
gc()  

